﻿using Models.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace yymbDAL
{
  public  class LikeService
    {
        /// <summary>
        /// 点赞的方法
        /// </summary>
        /// <returns></returns>
        public  int Fabulous(int Mid, int Userid)
        {
            
            using (yymbDBEntities db = new yymbDBEntities())
            {
                var Find = db.Likes.Where(l=>l.Like_Mbid==Mid&&l.Like_Uid==Userid).SingleOrDefault();
                
                if (Find==null)
                {
                    Likes like = new Likes { Like_Uid = Userid, Like_Mbid = Mid };
                    db.Likes.Add(like);
                    if (db.SaveChanges() > 0)
                    {
                        return 1;
                    }
                }              
                    db.Likes.Remove(Find);
                    db.SaveChanges();
                    return 0;              
            }
        }
        /// <summary>
        /// 评论的方法
        /// </summary>
        /// <returns></returns>
        public bool Commentary(Comment comment)
        {
            using (yymbDBEntities db = new yymbDBEntities())
            {
                db.Comment.Add(comment);
                db.SaveChanges();
                if (db.SaveChanges()>0)
                {
                    return true;
                }
                return false;
            }

        }
        /// <summary>
        /// 关注的方法
        /// </summary>
        /// <returns></returns>
        public int Follow(int Mid, int Userid)//Mid 当前页面ID   Userid登录用户ID
        {
            using (yymbDBEntities db = new yymbDBEntities())
            {
                var Find = db.User_rs.Where(u => u.UserID == Userid && u.Use_UserID == Mid).SingleOrDefault();
                if (Find == null)
                {
                    var time = DateTime.Now;
                    User_rs like = new User_rs { Creation_time = time, UserID = Userid, Use_UserID = Mid };
                    db.User_rs.Add(like);
                    if (db.SaveChanges() > 0)
                    {
                        return 1;
                    }
                }
                db.User_rs.Remove(Find);
                db.SaveChanges();
                return 0;
            }
        }
        /// <summary>
        /// 页面显示是否关注的方法
        /// </summary>
        /// <returns></returns>
        public bool Foll(int Mid, int Userid)//mid当前页面ID   userid
        {
            using (yymbDBEntities db = new yymbDBEntities())
            {
                var Find = db.User_rs.Where(u => u.UserID == Userid && u.Use_UserID == Mid).SingleOrDefault();
                if (Find != null || Mid == Userid)
                {
                    return true;

                }
                return false;
            }
        }
        /// <summary>
        /// 显示喜欢的数量
        /// </summary>
        /// <returns></returns>
        public int Like(int id)
        {
            using (yymbDBEntities db = new yymbDBEntities())
            {
                return db.Likes.Count(l => l.Like_Uid == id);
            }

        }

        /// <summary>
        /// 显示人气
        /// </summary>
        /// <returns></returns>
        public int popu(int id)
        {
            using (yymbDBEntities db = new yymbDBEntities())
            {
                var zan = db.Likes.Count(l => l.Like_Uid == id);
                var guan = db.User_rs.Count(u => u.Use_UserID == id);
                return zan + guan;
            }

        }
    }
}
